package thong.servlet;

import java.io.IOException;
import java.util.SortedMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.jstl.sql.Result;

import thong.util.JdbcUtil;

/**
 * Servlet implementation class OrderServlet
 */
@WebServlet("/OrderServlet")
public class OrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrderServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		/*
		 *  nhận tham số từ form nhập
		 */



        /* 
         * Làm việc với CSDL khi tồn tài các tham số điều khiển tương 
         * ứng (btnInsert, btnUpdate, btnDelete, btnSelect, btnSearch)
         */
        try{
	       if(request.getParameter("OderDetails") != null){
	    	   String OrderId = request.getParameter("OderDetails");
		        String sql = "SELECT * FROM OrderDetails WHERE OrderId="+OrderId ;
		        /*
		         * Truy vấn và chia sẻ kết quả trong request
		         */
		        //String id1=request.getParameter("lnkEdit");
		        Result orderd = JdbcUtil.toResult(sql);
		        
		        request.setAttribute("orderd", orderd);
		        request.setAttribute("errors", "Loaded successfully !");
	        }
        }
        catch (Exception e) {
        	request.setAttribute("errors", "Data manipilation error !");
        	//request.setAttribute("errors", "loi haha" + ProductDate);
        	//request.setAttribute("errors", e.getMessage());
		}
        /*
         * Duy trì chuỗi tìm kiếm của câu lệnh sql trong session
         */
        HttpSession session = request.getSession();
        if(request.getParameter("btnSearch") != null){
        	session.setAttribute("search", request.getParameter("txtSearch"));
        }
        else if(session.getAttribute("search") == null){
        	session.setAttribute("search", "");
        }
        String search = (String)session.getAttribute("search");
        
        /*
         * Tìm kiếm theo tên và chia sẽ kết quả trong request
         */
        String searchSql = "SELECT * FROM Orders  WHERE FullName LIKE ?";
        Result oder = JdbcUtil.toResult(searchSql, "%" + search + "%");
        request.setAttribute("orders", oder);
        /*
         * Chuyển tiếp sang trang ManagerProduct.jsp
         */
       // request.getRequestDispatcher("CustomerCRUD.jsp").forward(request, response);
        request.getRequestDispatcher("pages/ManagerOrders.jsp").forward(request, response);
	}

}
